package cn.wolfcode.shop.seckill.web.frontend;


import cn.wolfcode.cloud.redis.RedisService;
import cn.wolfcode.cloud.result.Result;
import cn.wolfcode.shop.member.domain.User;
import cn.wolfcode.shop.seckill.redis.SeckillKeyPrefix;
import cn.wolfcode.shop.seckill.result.SeckillGoodServerCodeMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
public class PathController {

    @Autowired
    private RedisService redisService;

    @RequestMapping("/getSecPath")
    public Result  getSecPath(User user, Long goodId, String uuid, Integer veryCodeVal){
        if(user ==null){
            return Result.error(SeckillGoodServerCodeMsg.USER_EMPTY);
        }
        //判断验证码是否合法
        if(veryCodeVal==null){
            return Result.error(SeckillGoodServerCodeMsg.VERYCODE_ERROR);
        }
        Integer oldVal = redisService.get(SeckillKeyPrefix.SECKILLVERIFYCODERESULT, uuid, Integer.class);
        if(oldVal ==null || !oldVal.equals(veryCodeVal)){
            return Result.error(SeckillGoodServerCodeMsg.VERYCODE_ERROR);
        }
        //开始生成一个地址
        String path= UUID.randomUUID().toString().replace("-","");
        redisService.set(SeckillKeyPrefix.SECKILLPATH,user.getId()+":"+goodId,path);
        return Result.success(path);
    }
}
